package com.manager.car.dao;


import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.orm.hibernate5.HibernateCallback;

import com.manager.car.po.SubscribeInfo;
import com.manager.car.util.PageHibernateCallback;


public class SubscribeDaoImpl extends BaseDao<SubscribeInfo> implements ISubscribeDao {
   
	//查询总记录书
	public int getTotalRecord(String condition, Object[] params) {
		String hql = "select count(*) from SubscribeInfo where 1=1 "
				+ condition;
		//System.out.println(condition+"flsk"+params.length);
		System.out.println(hql+"jslfdj");
		List<Long> list = (List<Long>) this.getHibernateTemplate().find(hql);
		return list.get(0).intValue();

	}
   //分页查询
	public List<SubscribeInfo> findAll(String condition, Object[] params,  final int startIndex, final int pageSize) {
		String hql = " from SubscribeInfo where 1=1 " + condition;
		System.out.println(hql);
		return  this.getHibernateTemplate().execute(
				new PageHibernateCallback<SubscribeInfo>().setHql(hql)
				.setParams(null).setPageSize(pageSize)
				.setStartIndex(startIndex));
		
	
	}
   //分页查询
	public List<SubscribeInfo> queryAll(final String name,final int limit,final int page){
		@SuppressWarnings("unchecked")
		List<SubscribeInfo> list=(List<SubscribeInfo>) this.getHibernateTemplate().execute(new HibernateCallback<Object>() {

			public Object doInHibernate(Session session) throws HibernateException {
				Query<SubscribeInfo> query=null;
				
				//未屏蔽
				 if(name.equals("s") ) {
				 query=session.createQuery(" from SubscribeInfo where subStatic=0 order by subTime asc ");
				}
				//已屏蔽
				else if(name.equals("w")) {
					query=session.createQuery(" from SubscribeInfo where subStatic=1 order by subTime asc ");
				}
				else if(name.substring(name.length()-1,name.length()).equals("s")) {
					query=session.createQuery(" from SubscribeInfo where subStatic=0 and subName like ? order by subTime asc ").setParameter(0, "%"+ name.substring(0,name.length()-1)+"%");
				}
				else if(name.substring(name.length()-1,name.length()).equals("w")) {
					query=session.createQuery(" from SubscribeInfo where subStatic=1 and subName like ? order by subTime asc ").setParameter(0, "%"+ name.substring(0,name.length()-1)+"%");
				}
				int begin=(page-1)*limit;
				query.setFirstResult(begin);
				query.setMaxResults(limit);
				return query.list();
			}
		});
		
		return list;
	}
	//总记录数
	public int  getcount(final String name,int limit, int page) {
		int count=(Integer) this.getHibernateTemplate().execute(new HibernateCallback<Object>() {

			@SuppressWarnings("unchecked")
			public Object doInHibernate(Session session) throws HibernateException {
				Query<SubscribeInfo> query=null;
				//未屏蔽
				if(name.equals("s") ) {
				 query=session.createQuery(" from SubscribeInfo where subStatic=0 ");
				}
				//已屏蔽
				else if(name.equals("w")) {
					query=session.createQuery(" from SubscribeInfo where subStatic=1 ");
				}
				else if(name.substring(name.length()-1,name.length()).equals("s")) {
					query=session.createQuery(" from SubscribeInfo where subStatic=0 and subName like ? order by subTime asc ").setParameter(0, "%"+ name.substring(0,name.length()-1)+"%");
				}
				else if(name.substring(name.length()-1,name.length()).equals("w")) {
					query=session.createQuery(" from SubscribeInfo where subStatic=1 and subName like ? order by subTime asc ").setParameter(0, name.substring(name.length()-1));
				}
				
				return query.list().size();
			}
		});
		return count;
	}
     //添加
	public void addSub(SubscribeInfo sub) {
		this.save(sub);
		
	}
    //修改
	public void updateSub(SubscribeInfo sub) {
		this.update(sub);
		
	}
    //删除
	public void deleteSub(Integer id) {
		this.delete(id);
		
	}
     //根据ID查询
	public SubscribeInfo queryBysubId(Integer id) {
		
		return queryById(id);
	}
    


}
